/**
 * 
 */
package com.example.gensurdatviwi.application.controller;

import java.io.Serializable;

import com.example.gensurdatviwi.application.datamodel.SurveyDatabaseFieldList;
import com.example.gensurdatviwi.application.datamodel.ToolBoxCollection;
import com.example.gensurdatviwi.layout.ApplicationWindowPanel;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.event.dd.DragAndDropEvent;
import com.vaadin.event.dd.DropHandler;
import com.vaadin.event.dd.acceptcriteria.AcceptAll;
import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.DragAndDropWrapper.WrapperTargetDetails;
import com.vaadin.ui.DragAndDropWrapper.WrapperTransferable;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.TabSheet.SelectedTabChangeEvent;
import com.vaadin.ui.TabSheet.SelectedTabChangeListener;
import com.vaadin.ui.TabSheet.Tab;



/**
 * Controller class of the entire application.
 * 
 * @author Michael Hausegger
 *
 */
public class Controller implements ValueChangeListener, SelectedTabChangeListener, DropHandler, ClickListener, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7071696525870004342L;
	
	
	private ApplicationWindowPanel applicationWindowPanel;

	
	private SurveyDatabaseFieldList  currentWorkingDBFieldList = new SurveyDatabaseFieldList();
	
	
	/**
	 * @return the currentWorkingDBFieldList
	 */
	public SurveyDatabaseFieldList getCurrentWorkingDBFieldList() {
		return currentWorkingDBFieldList;
	}


	/**
	 * 
	 */
	public void valueChange(ValueChangeEvent event) {

		System.out.println("123");
		
        if ( event.getProperty() != null &&
        event.getProperty().getValue() != null) {
        	
        	System.out.println(event.getProperty().getValue());
        	
        	//this.applicationWindowPanel.setNavigationContainerContent(new Label("Hallo"));
        	        
        
        }
		
		
	}


	/**
	 * 
	 * @param applicationWindowPanel
	 */
	public void setApplicationWindowPanel(ApplicationWindowPanel applicationWindowPanel) {

		this.applicationWindowPanel = applicationWindowPanel;
		
	}




	public void selectedTabChange(SelectedTabChangeEvent event) {
		
		System.out.println("Listener");
		
		TabSheet tabsheet = event.getTabSheet();
		Tab tab = tabsheet.getTab(tabsheet.getSelectedTab());
		
		if (tab != null) {
			//getWindow().showNotification("Selected tab: " + tab.getCaption());
			System.out.println("Selected tab: " + tab.getCaption());
		}
	}


	public void drop(DragAndDropEvent event) {

		
	    WrapperTransferable  sourceComponent = (WrapperTransferable) event.getTransferable();
	    WrapperTargetDetails targetComponent = (WrapperTargetDetails) event.getTargetDetails();		
		
	    Long idToAdd       =  new Long( sourceComponent.getDraggedComponent().getDebugId() );
	    Long idWhereToAdd  =  new Long( targetComponent.getTarget().getDebugId() );
	    
	        
//	    System.out.println( sourceComponent.getDraggedComponent().getDebugId() );
	        
//	    System.out.println( targetComponent.getTarget().getDebugId() );
		
	    //System.out.println("Origin: " + idToAdd);
	    //System.out.println("idWhereToAdd: " + idWhereToAdd);

	    
	    //We know that this is an new field to add.
	    if ( idToAdd >= 0 ){
		    this.currentWorkingDBFieldList.addDatabaseFieldAtPosition(ToolBoxCollection.getSupportedSurveyDatabaseFieldById(idToAdd), idWhereToAdd );	    	
	    }
	    //We know that this is a shift operation within the current list of fields.
	    else{
	    	this.currentWorkingDBFieldList.moveDatabaseFieldFromPositionToPosition(idToAdd * -1 - 1, idWhereToAdd );
	    }
	    
	    
	    applicationWindowPanel.showSurveyDatabaseWorkingFieldView();
	    
	}


	public AcceptCriterion getAcceptCriterion() {

		//System.out.println("Accept Hallo");
		
		return AcceptAll.get();
		
	}


	public void buttonClick(ClickEvent event) {
		
		//System.out.println(123);
		
		Button sourceButton = event.getButton();
		
		//User wants to start to build a new survey database.
		if( ( sourceButton.getDebugId().equals( getCreateNewSurveyDatabaseButtonDebugId() ) ) )  {
		
			this.currentWorkingDBFieldList = new SurveyDatabaseFieldList();			
			this.applicationWindowPanel.showSurveyDatabaseWorkingFieldView();
			
		}
		
		
	}


	
	
	public static String getCreateNewSurveyDatabaseButtonDebugId() {
		
		return "CreateNewSurveyDatabaseButtonDebugId";
	
	}



	
	
}
